<?php namespace Home\Controller; 
use Hdphp\Controller\Controller;
/**
 * 列表控制器
 */
class ListController extends Controller {
	
	public function index()
	{
		$cid=Q('get.cid',0,'intval');
		$datat=Db::table('Category')->where("pid=0")->orderBy('csort','DESC')->limit(8)->get();
		//根据分类获得商品gid
        $gids=$this->cidGetGid($cid);
		 //根据gids获得商品属性（筛选属性）
        $goodsAttr=$this->getGoodsAttr($gids);
        View::with('goodsAttr',$goodsAttr);
		//筛选地址处理，筛选参数param处理
		$num=count($goodsAttr);
        if($num){
            $param=isset($_GET['param'])?explode('_', $_GET['param']):array_fill(0, $num, 0);   
        }else{//如果分类下面没有商品属性的时候
            $param=array();
        }
		//根据商品属性id进行筛选商品
        $finalGids=$this->filterGid($param,$gids);
        View::with('param',$param);
		$in=isset($finalGids)?"in (".implode(',', $finalGids).")":'';
		$datalist=Db::table('goods')->where("Category_cid={$cid} and gid {$in}")->get();
		View::with('datat',$datat);
		View::with('datalist',$datalist);
		View::make();
	}
	private function filterGid($param,$cidGids){
        $gids=array();
        foreach ($param as $gtid) {
            //如果不是不限的时候
            if($gtid){
                //通过taid获得属性的名称
               $name=Db::table('goods_att')->where("gaid={$gtid}")->pluck('gavalue');
               //通过属性名称查找商品gid
               $gids[]=Db::table('goods_att')->where("gavalue='{$name}'")->lists('goods_gid');
            }
			
        }
        //如果有点击任何一个筛选属性的时候
        if($gids){
            //把获得好的商品gid取交集
            $gidsOne=$gids[0];
            foreach ($gids as $v) {
                $gidsOne=array_intersect($v, $gidsOne);
            }
            //再和分类查找出来的$cidGids取交集
            $finalGids=array_intersect($gidsOne, $cidGids);
            return $finalGids;
        }else{
        	//如果全部点击不限的时候
            return $cidGids;
        }
       
    }
	private function getGoodsAttr($gids)
	{
		 if($gids){
            $goodsAttr = Db::table('goods_att')->where("goods_gid in(" . implode(',', $gids) . ")")->groupBy('gavalue')->get();
		  	$temp1 = array();
            foreach ($goodsAttr as $v) {
               $temp1[$v['type_att_taid']][] = $v;
            }
			foreach ($temp1 as $taid => $value) {
               $name = Db::table('type_att')->where("taid={$taid}")->pluck('taname');
               $temp2[] = array(
                    'name' =>  $name,
                    'value'=> $value
                );
            }
			return $temp2;
        }else{
            return array();
        }
	}
	 //根据分类筛选商品
	 private function cidGetGid($cid)
	 {
	 	$cids = $this->getSon(Db::table('category')->get(),$cid);
        $cids[] = $cid;
		$gids = Db::table('goods')->where("category_cid in (".implode(',', $cids).")")->lists('gid');
		return $gids;
	 }
	 //获取分类的子集
	 private function getSon($data,$cid)
	 {
	 	$temp = array();
        foreach ($data as $v) {
            //如果找到子集
          if($v['pid'] == $cid){
             $temp[] = $v['cid'];
             $temp = array_merge($temp,$this->getSon($data,$v['cid']));
          }
        }
        return $temp;
	 }

}

?>